
// oss上传文件

import { useStore } from '@/store'
import OSS from 'ali-oss';
const store = useStore();
// 批量上传图片
// export const uploadMultiImage = async(files:Array<any>) => {
//     const tokenRes:boolean = await store.dispatch(UserActionTypes.ACTION_OSS_TOKEN, null);
//     if(tokenRes) {
//         console.log(store.state.user.ossConfig)
//     } else {

//     }
// }

export const uploadImage = async (file) => {
    try {
        const ossClient = new OSS(store.state.user.ossConfig);
        const id = file.name.split('.')[0];
        const result: any = await ossClient
            .put(`${import.meta.env.MODE}/images/${store.state.user.company.compCode}/${file.name}`, file);
        if (result.res.statusCode === 200) {
            return { success: true, data: { url: result.name, fullUrl: result.url } }
        } else {
            return { success: false }
        }

    } catch (err) {
        return { success: false }
    }



    // .then((result: any) => {
    //   if (result.res.statusCode === 200) {
    //     // 上传成功
    //     console.log(result);
    //     observer.next({ name: result.name, url: result.url, id } as OssUploadRes);
    //   } else {
    //     observer.error();
    //   }
    // })
    // .catch(err => {
    //   observer.error();
    // });
}